package mas;

import boris.utils.ArgList;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;

/** Class provides a GUI for the sending and receiving of Messages between 
 * agents
 * 
 * Advanced Java Programming - ICA 2 Middleware 
 * @author Carl Morphet, Matt Constable, Stephen Pammenter
 * Teesside University
 * @version %I% 
 * Created: Apr 19, 2012
 */
public class Chat3Client extends JFrame
{
    /** Components and instance variables */
    private String name, room, roomManager;
    private Message message;
    TextArea msgArea;
    
    /** The default identifier for a message, incremented each time a message 
     * is sent */
    private int messageID = 0;

    /** Overloaded constructor for the Chat3Client, takes a portal and an
     * identifier for the client as parameters, handles all major functionality 
     * of the class
     * 
     * @param portal The portal associated with this instance of Chat3Client
     * @param clientName The clients identifier 
     */
    public Chat3Client(Portal portal, String clientName)
    {	
        super();
        this.name = clientName;

        if (name == null)
        {	
            name = "user1";
            sysout("WARNING: no name supplied, naming as " + name);
        }
        if (room == null)
        {	
            room = "room1";
            sysout("WARNING: no room supplied, using " + room);
        }
        roomManager = room + "manager";

        //--- portal -------------------------
        //final Portal portal1 = new Portal(name + "/P");
        //portal1.connectToGrid( 1234 );
        //portal1.waitForConnection();

       /** In line override of the msgHandler method to add the message to the
         * GUI */
       final UserAgent agent = new UserAgent(name)
       {   
            @Override
            public void msgHandler (Message message)
            {
                msgArea.append("\n" + message.getMessage());
            }
       };
       
        portal.registerAgent(agent.getName(), agent);

        /** GUI Setup */
        setTitle("ChatClient: " + name);

        Container main = getContentPane();
        main.setLayout( new BorderLayout());

        Container topLine = new Container();
        topLine.setLayout(new FlowLayout());
        Container btmLine = new Container();
        btmLine.setLayout(new FlowLayout());

        main.add(topLine, BorderLayout.NORTH);
        main.add(btmLine, BorderLayout.SOUTH);

        msgArea = new TextArea(12, 60);
        main.add(msgArea, BorderLayout.CENTER);

        /** Creation, instantiation and event handling for the join button */
        Button joinBtn = new Button("join");
        joinBtn.addActionListener(new ActionListener()
        {	
            @Override
            public void actionPerformed(ActionEvent e)
            {	
                message = new Message(null, null, roomManager, "join");
                agent.msgHandler(message);
            }
        });
        topLine.add(joinBtn);

        /** Creation, instantiation and event handling for the leave button */
        Button leaveBtn = new Button("leave");
        leaveBtn.addActionListener(new ActionListener()
        {	
            @Override
            public void actionPerformed(ActionEvent e)   
            {	
                message = new Message(null, null, roomManager, "leave");
                agent.msgHandler(message);
            }
        });
        topLine.add(leaveBtn);

        /** The primary output for messages for the Chat3Client GUI */
        final TextField chatMsg = new TextField(20);
        btmLine.add(chatMsg);

        /** Creation, instantiation and event handling for the send button */
        Button sendBtn = new Button("send");
        sendBtn.addActionListener(new ActionListener()
        {	
            @Override
            public void actionPerformed(ActionEvent e)
            {	
                //NOTES: Have a look at his agent.sendMessage() to see the parameters it takes 
                //Check the new message created matches his destination etc. 
                //Do the same with the leave button 
                message = new Message(String.valueOf(messageID), name, room, chatMsg.getText());
                agent.msgHandler(message);
                messageID ++;
            }
        });
        btmLine.add(sendBtn);

        /** Setting the size and visibility of the JFrame */
        pack();
        setVisible(true);
    }

    /** Method which takes a String and prints it to console with additional 
     * formatting
     * 
     * @param string The string to be printed
     */
    private void sysout(String string)
    {	
        System.out.println("[" + name + "] " + string);
    }
}